﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace OfflineReport
{
    public partial class frBaoCaoThuNo : Form
    {
        string str_cnn = ConfigurationManager.ConnectionStrings["Report.Properties.Settings.BaoCaoConnectionString"].ConnectionString;
        int type = 0;
        SQL sql = new SQL();
        //SqlHelper sql_helper = new SqlHelper();

        string store_pr = "sql_BaoCaoThuNo";
        public frBaoCaoThuNo()
        {
            InitializeComponent();
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            type = getLHKH(cbLdKh.Text.Trim());
            SqlParameter[] pr = new SqlParameter[6];
            pr[0] = new SqlParameter("@CURRENCY", cbCurr.Text.Trim());
            pr[1] = new SqlParameter("@LoaiKH", type);
            pr[2] = new SqlParameter("@LDLINK", cbLdlink.Text.Trim());
            pr[3] = new SqlParameter("@MaKH", cbIdKh.Text.Trim());
            pr[4] = new SqlParameter("@dateFrom", dateTimePicker1.Value.ToShortDateString());
            pr[5] = new SqlParameter("@dateTo", dateTimePicker2.Value.ToShortDateString());
            DataTable dt = new DataTable();
            SqlHelper.ExecuteDataTable(str_cnn, CommandType.StoredProcedure, store_pr, dt, pr);
            dt.Columns.Add("PR", typeof(double));
                dt.Columns.Add("IN", typeof(double));
                dt.Columns.Add("PE", typeof(double));
                dt.Columns.Add("PS", typeof(double));
                string typePR = "PR";
                string typeIN = "IN";
                string typePE = "PE";
                string typePS = "PS";
                foreach (DataRow dr in dt.Rows)
                {
                    string reportDate = dr["REPORT_DATE"].ToString();
                    string ldID = dr["LD_ID"].ToString();
                   
                    dr["PR"] = sql.getData(ldID, reportDate, typePR);
                    dr["IN"] = sql.getData(ldID, reportDate, typeIN);
                    dr["PE"] = sql.getData(ldID, reportDate, typePE);
                    dr["PS"] = sql.getData(ldID, reportDate, typePS);
                }

            Data ds1 = new Data();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ds1.Tables["DT"].Rows.Add(dt.Rows[i]["ID_KH"], dt.Rows[i]["NAMEKH"], dt.Rows[i]["TYPE_KH"], dt.Rows[i]["N_TYPEKH"], dt.Rows[i]["REPORT_DATE"], dt.Rows[i]["LD_ID"], dt.Rows[i]["CURRENCY"], dt.Rows[i]["LD_LINK"], dt.Rows[i]["PR"], dt.Rows[i]["IN"], dt.Rows[i]["PE"], dt.Rows[i]["PS"]);
            }

            OfflinReport rp = new OfflinReport();
            rp.SetDataSource(ds1);

            ParameterFieldDefinitions crParameterFieldDefinitions;
            ParameterFieldDefinition crParameterFieldDefinition;
            ParameterValues crParameterValues = new ParameterValues();
            ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();
            crParameterDiscreteValue.Value = dateTimePicker1.Value.ToShortDateString();
            crParameterFieldDefinitions = rp.DataDefinition.ParameterFields;
            crParameterFieldDefinition = crParameterFieldDefinitions["dateFrom"];
            crParameterValues = crParameterFieldDefinition.CurrentValues;           
            crParameterValues.Clear();
            crParameterValues.Add(crParameterDiscreteValue);
            crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

            ParameterFieldDefinitions crParameterFieldDefinitions1;
            ParameterFieldDefinition crParameterFieldDefinition1;
            ParameterValues crParameterValues1 = new ParameterValues();
            ParameterDiscreteValue crParameterDiscreteValue1 = new ParameterDiscreteValue();
            crParameterDiscreteValue1.Value = dateTimePicker2.Value.ToShortDateString();
            crParameterFieldDefinitions1 = rp.DataDefinition.ParameterFields;
            crParameterFieldDefinition1 = crParameterFieldDefinitions1["dateTo"];
            crParameterValues1 = crParameterFieldDefinition1.CurrentValues;
            crParameterValues1.Clear();
            crParameterValues1.Add(crParameterDiscreteValue1);
            crParameterFieldDefinition1.ApplyCurrentValues(crParameterValues1);


            crystalReportViewer1.ReportSource = rp;
            crystalReportViewer1.Refresh();
        }
        private int getLHKH(string type_kh)
        {
            int n = 0;
            switch (type_kh)
            {
                case "All":
                    n=0;
                    break;
                case "Cá nhân":
                    n = 1;
                    break;
                case "Doanh nghiệp":
                    n = 2;
                    break;
                default:
                    n = 0;
                    break;
                    
            }
            return n;
        }
        
    }
}
